//
//  ViewController.m
//  UIHomeWork20_XML解析
//
//  Created by tusm on 16/8/10.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Album.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic,strong)NSMutableArray *albumArray;
@property (nonatomic,copy)NSString *currentElementName;

@property (nonatomic,strong)NSMutableArray *domAlbumArray;

@property (nonatomic,copy)NSString *element;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
}

- (IBAction)SAXPaser:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
    NSData *pathData = [NSData dataWithContentsOfFile:path];
    NSXMLParser *xmParser = [[NSXMLParser alloc] initWithData:pathData];
    xmParser.delegate = self;
    [xmParser parse];

}
//1.解析开始
- (void)parserDidStartDocument:(NSXMLParser *)parser{
//初始化数组
    self.albumArray = [NSMutableArray array];

}
//2.遇到节点头调用
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    
//    NSLog(@"%@",elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"AlbumInfo"]) {
        Album *album = [Album new];
        [_albumArray addObject:album];
    }

}

//3.处理节点值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (_currentElementName) {
        Album *album = [_albumArray lastObject];
        [album setValue:string forKey:_currentElementName];
    }

}
//4.遇到节点尾调用
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    
    _currentElementName = nil;

}
//5.解析结束
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    
    NSLog(@"%@",_albumArray);

}

//6.解析错误
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"解析错误:%@",parseError);

}


- (IBAction)DOMPreser:(id)sender {
    self.domAlbumArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
    NSData *pathData = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    //1.创建GDataXMLDocument对象
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:pathData options:0 error:&error];
    //2.根据document获取根节点
    GDataXMLElement *root = [document rootElement];
    NSArray *rootElement = root.children;
    
    for (GDataXMLElement *childElement in rootElement) {
        _element = childElement.name;
        for (GDataXMLElement *sunElement in childElement.children) {
            Album *album = [Album new];
            if ([_element isEqualToString:@"page"]) {
                [album setValue:sunElement.stringValue forKey:sunElement.name];
            }
            [self.domAlbumArray addObject:album];
            for (GDataXMLElement *grandElement in sunElement.children) {
                [album setValue:grandElement.stringValue forKey:grandElement.name];
            }
        }
    }
    NSLog(@"%@", _domAlbumArray);
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
